package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.form.PageRequest;
import com.cskaoyan.shopping.service.IPanelService;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 曾南天负责的模块controller
 * 包含商品模块下的查看商品详情、分页查询商品列表和查询推荐商品等接口
 *
 * @author mabai
 * @return
 * @since 2022/10/25 19:49
 */
@RestController
public class ShoppingControllerByZnt {


    @Autowired
    IProductService iProductService;

    @Autowired
    IPanelService iPanelService;

    /**
     * 查看商品详情
     *
     * @param productId item表的主键id
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author mabai
     * @since 2022/10/26 10:00
     */
    @GetMapping("/shopping/product/{productId}")
    public ResponseData getProductDetail(@PathVariable String productId) {
        Long id = null;
        try {
            id = Long.parseLong(productId);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ResponseUtil().setErrorMsg("请求数据格式非法");
        }
        ProductDetailDto productDetail = iProductService.getProductDetail(id);

        return new ResponseUtil().setData(productDetail);
    }

    /**
     * 查询推荐商品
     *
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author mabai
     * @since 2022/10/26 10:02
     */
    @GetMapping("/shopping/recommend")
    public ResponseData getRecommendProduct() {
        // TODO:重构前的代码
        /*// 根据推荐商品板块表id查询
        List<PanelDto> result = iPanelService.getRecommendProductByRecommendPanelId(GlobalConstants.RECOMMEND_PANEL_ID);*/
        // return new ResponseUtil().setData(result);
        // TODO:重构后的代码
        Set<PanelDto> result = iProductService.getRecommendGoods();
        return new ResponseUtil().setData(result);
    }

    /**
     * 分页查询商品列表
     *
     * @param pageRequest page页码、size每页条数、sort是否排序、priceGt价格最小值、priceLte价格最大值、cid
     * @return com.cskaoyan.mall.commons.result.ResponseData
     * @author mabai
     * @since 2022/10/26 15:59
     */
    @GetMapping("/shopping/goods")
    public ResponseData getProductList(PageRequest pageRequest) {
        AllProductResponse result = iProductService.getAllProduct(pageRequest);
        return new ResponseUtil().setData(result);
    }
}
